public without sharing class CooperationStandardHandler {
   	////设置跟踪总表的归属公司 ,取开票客户的所有人对应的公司 
   /* public static void  setCompany(List<CooperationStandard__c> list_new ){

    	Map<Id,String> accMap = new Map<Id,String>();
    	for (CooperationStandard__c cs:list_new ) {
    		
    		if(cs.Account__c != null )accMap.put(cs.Account__c, null);
    	}

    	  ///匹配公司
         Map<String ,String> comMap = ImportObject.getPicklistValues('CooperationStandard__c','company__c',false);

    	for(Account acc: [select  Id, Owner.SubCompany__c from Account where Id in:accMap.keySet()]){

    		accMap.put(acc.Id, comMap.get(acc.Owner.SubCompany__c));
    	}

    	for (CooperationStandard__c cs  : list_new) {
    		if(cs.Account__c != null )cs.company__c = accMap.get(cs.Account__c);
    	}

    }*/


    public static void setAccount(List<CooperationStandard__c> list_new,Map<Id,CooperationStandard__c> oldMap ){

        Set<Id> accsets = new Set<Id>();
        for ( CooperationStandard__c coo : list_new) {
            
            CooperationStandard__c old = oldMap == null ? null : oldMap.get(coo.Id);
            if(old == null || coo.Account__c != old.Account__c  ){
                accsets.add(coo.Account__c);
            } 


        }
        if(accsets.size() == 0 ) backAccount(accsets);
    }

    public static void backAccount(Set<Id> accsets ){

        
        List<Account> accList = new List<Account>();

        for (AggregateResult arg  : [select COUNT_DISTINCT(Company__c)  ,Account__c , Account__r.IFuserule__c  
            from CooperationStandard__c where  Account__c in: accsets
            group by Account__c ,Account__r.IFuserule__c having COUNT_DISTINCT(Company__c)  > 1 ] ) {
            
           Boolean ifu = arg.get('IFuserule__c') == null ? true : !Boolean.valueOf(arg.get('IFuserule__c'));
                        
            accList.add(new Account(Id=String.valueOf(arg.get('Account__c')),IFuserule__c=ifu,IfMoreCompany__c = true  ) );
        } 

        if(accList.size() > 0 ) update accList;
   }
}